package com.intgrt.manager.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.intgrt.manager.entity.SysUser;
import com.intgrt.manager.midentity.PswInfo;
import com.intgrt.manager.service.SysUserService;
import com.intgrt.manager.service.base.impl.BaseServiceImpl;
import com.intgrt.manager.util.ProjectConstants;
import com.intgrt.manager.util.ProjectTools;

@Service
public class SysUserServiceImpl extends BaseServiceImpl implements SysUserService {

	@Override
	public SysUser getNameUser(String usrName) {
		String sql = "select id, orgid, roleid, acccode, accpsw, accname, accphone, accemail from sys_user"
				+ " where acccode = '"+usrName+"'";
		List<Map<String,Object>> reslst = this.getMapList(sql);
		if(reslst.size()>0){
			SysUser user = getEntity(reslst.get(0));
			sql = "select menuid, buttons from sys_auth where roleid = "+user.getRoleid();
			reslst = this.getMapList(sql);
			JSONObject userMenuBtns = new JSONObject();
			if(reslst.size()>0){
				for(Map<String,Object> map:reslst){
					String menuid = String.valueOf(map.get("menuid"));
					userMenuBtns.put(menuid, map.get("buttons"));
				}
			}
			user.setMenuButtons(userMenuBtns);
			return user;
		}
		return null;
	}
	
	@Override
	public int updSelf(SysUser user) {
		String sql = "update sys_user set accname = '" + user.getAccname() + "', accphone = '"
				+ user.getAccphone() +"', accemail = '" + user.getAccemail() +"' where id = " + user.getId();
		return this.execute(sql);
	}
	
	@Override
	public String updPsw(String accid, PswInfo info) {
		String sql = "sys_user where id="+accid+" and accpsw = '"+ProjectTools.md5Encode(info.getOldpsw())+"'";
		long accnum = this.getCount(sql);
		if(1==accnum){
			sql = "update sys_user set accpsw='"+ProjectTools.md5Encode(info.getNewpsw())+"', accorign = '"
					+info.getNewpsw()+"' where id="+accid;
			return this.execute(sql)+"";
		}else{
			return "1";
		}
	}
	
	@Override
	public String getList(SysUser user, String orgid) {
		String content = "a.id,a.orgid,a.roleid,a.acccode,a.accname,a.accphone,a.accemail,"
				+ "a.crttm,a.state,b.orgname,c.rolename";
		String cdtsql = "sys_user a left join sys_organize b on a.orgid = b.id"
				+ " left join sys_role c on a.roleid = c.id where 1=1 ";
		String orgids = this.getSysOrgids(orgid);
		cdtsql += " and a.orgid in ("+orgids+")";
		if(ProjectTools.isNotEmpty(user.getAccname())){
			cdtsql += " and a.accname like '%"+user.getAccname()+"%'";
		}
		return getPageDate(content, cdtsql, user.getPage(), user.getRows()).toJSONString();
	}
	
	@Override
	public String onlyCode(String usrcode) {
		String cdtsql = "sys_user where acccode = '"+usrcode+"'";
		return this.getCount(cdtsql)+"";
	}
	
	@Override
	public String saveEntity(SysUser entity) {
		String sql = "insert into sys_user (orgid,roleid,acccode,accpsw,accname,accphone,accemail,accorign,crttm,state) values ("
				+ entity.getOrgid()+","+entity.getRoleid()+",'"+entity.getAcccode()+"','"+ProjectConstants.DEFAULT_MD5PSW+"','"
				+entity.getAccname()+"','"+entity.getAccphone()+"','"+entity.getAccemail()+"','"+ProjectConstants.DEFAULT_PSW
				+"',"+ProjectTools.curTime()+","+entity.getState()+")";
		return this.execute(sql)+"";
	}

	@Override
	public String editEntity(SysUser entity) {
		String sql = "update sys_user set orgid = "+entity.getOrgid()+", roleid = "+entity.getRoleid()
			+", accname = '"+entity.getAccname()+"'"+", accphone = '"+entity.getAccphone()+"', accemail = '"
			+entity.getAccemail()+"', state="+entity.getState()+" where id = "+entity.getId();
		return this.execute(sql)+"";
	}

	@Override
	public String delEntity(String ids) {
		String sql = "delete from sys_user where id in ("+ids+")";
		return this.execute(sql)+"";
	}
	
	private static SysUser getEntity(Map<String,Object> resmap){
		SysUser entity = new SysUser();
		entity.setId(String.valueOf(resmap.get("id")));
		entity.setOrgid(String.valueOf(resmap.get("orgid")));
		entity.setRoleid(String.valueOf(resmap.get("roleid")));
		entity.setAcccode(String.valueOf(resmap.get("acccode")));
		entity.setAccpsw(String.valueOf(resmap.get("accpsw")));
		entity.setAccname(String.valueOf(resmap.get("accname")));
		entity.setAccphone(String.valueOf(resmap.get("accphone")));
		entity.setAccemail(String.valueOf(resmap.get("accemail")));
		return entity;
	}

}
